<template>



  <el-dialog :title="title" width="800px" :visible.sync="visible" destroy-on-close @close="closeCallback">
    <div class="card">
    <p class="title"><i class="fa fa-th-large fa-lg"></i>个人资料</p>
    <el-form :model="ruleForm2" status-icon :rules="rules" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户账号" prop="adminAccount">
        <el-input v-model="ruleForm2.adminAccount" autocomplete="off" ></el-input>
      </el-form-item>

      <el-form-item label="我的头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8001/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="ruleForm2.adminHeadProtrait" :src="ruleForm2.adminHeadProtrait" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="手机号" prop="adminPhoneNumber">
        <el-input v-model="ruleForm2.adminPhoneNumber" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="创建时间" prop="adminCreationTime">
        <el-date-picker
          v-model="ruleForm2.adminCreationTime"
          type="datetime"
          readonly
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="更新人" prop="updateBy">
        <el-input v-model="ruleForm2.updateBy" autocomplete="off" readonly></el-input>
      </el-form-item>

      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker
          v-model="ruleForm2.updateTime"
          type="datetime"
          readonly
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">确 定</el-button>
        <el-button @click="resetForm('ruleForm2')">重 置</el-button>
      </el-form-item>
    </el-form>
    </div>
  </el-dialog>
</template>

<script>
  import axios from "axios"
  import moment from "moment"
  import Cookie from "js-cookie"
export default {
  name: "userInfo",
  props: {
    title: {
      type: String,
      default: "账号信息"
    },
    dialogVisible: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: ""
    }
  },
  data () {
    return {
      roleName: false,
      roleData: "",
      visible: this.dialogVisible,
      ruleForm2: {
        mobilePhone: "",
        username: "",
        password: "",
        checkPass: "",
        roleId: "",
        status: "",
        sex: "1",
        age: 0,
        name: "",
        avatar: ""
      },
      rules: {
        adminAccount: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            "pattern": "a-zA-Z0-9_\u4e00-\u9fa5]{4,16})$",
            "message": "2 个汉字或 4-16 位字母,数字,汉字,下划线"
          }
        ],
        adminPhoneNumber: [
          {required: true, message: "请输入手机号", trigger: "blur" },
          {
            "pattern": "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(16[2|6|7])|(18[0-9])|(17([0-1]|[3]|[5-8]))|(19[1|8|9]))\\d{8}$",
            "message": "手机号格式不正确"
          }
        ],

      }
    }
  },
  created () {
    this.getAdminList();
  },
  methods: {
    getAdminList(){
      let admin = Cookie.get("admin")
      this.ruleForm2 = JSON.parse(admin);
        this.ruleForm2.adminCreationTime = moment(this.ruleForm2.adminCreationTime).format("YYYY-MM-DD HH:mm:ss");
        this.ruleForm2.updateBy = this.ruleForm2.adminAccount;
        this.ruleForm2.updateTime = moment(this.ruleForm2.updateTime).format("YYYY-MM-DD HH:mm:ss");
      console.log(this.ruleForm2);
    },
    handleAvatarSuccess (res, file) {
      this.ruleForm2.adminHeadProtrait = res;
    },
    closeCallback () {
        this.$emit("successCallback")
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === "image/jpeg"
      const isPNG = file.type === "image/png"
      const isLt2M = file.size / 1024 / 1024 < 1

      if (!(isJPG || isPNG)) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!")
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 1MB!")
      }
      // eslint-disable-next-line no-mixed-operators
      return isLt2M && isJPG || isPNG
    },
    getList () {
      let that = this
      this.$request.fetchGetRoleList().then(function (response) {
        that.roleData = response.data.rows
        let userId = that.userId
        if (!userId) {
          return false
        }
        that.$request.fetchGetUserInfoId({id: userId})
          .then(function (res) {
            console.log(res)
            res.data.password = ""
            if (res.data.status === "1") {
              res.data.status = true
            } else {
              res.data.status = false
            }

            that.ruleForm2 = res.data
            that.roleName = true
            for (let i = 0; i < that.roleData.length; i++) {
              if (that.$store.getters.info.role === "超级管理员" && that.$store.getters.info.uid !== userId) {
                that.roleName = false
              }
            }
            return false
          })
          .catch(function (error) {
            console.log(error)
          })
      })
        .catch(function (error) {
          console.log(error)
        })
    },
    submitForm (formName) {

      this.ruleForm2.adminCreationTime = moment(this.ruleForm2.adminCreationTime).format("YYYY-MM-DD HH:mm:ss");
      this.ruleForm2.updateBy = this.ruleForm2.adminAccount;
      this.ruleForm2.updateTime = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");

      let data = {

        adminId: this.ruleForm2.adminId,
        adminAccount: this.ruleForm2.adminAccount,
        adminHeadProtrait: this.ruleForm2.adminHeadProtrait,
        adminPhoneNumber: this.ruleForm2.adminPhoneNumber,
        adminCreationTime:  this.ruleForm2.adminCreationTime,
        updateBy: this.ruleForm2.adminAccount,
        updateTime: this.ruleForm2.updateTime

      };

      console.log(this.ruleForm2);
      this.$axios({
        method: 'put',
        url: 'http://localhost:8001/admin',
        data: data
      }).then( resu => {
        if (resu){

          this.$message({
            message: '修改完成',
            type: 'success'
          });

          axios.get('http://localhost:8001/admin/adminSign',{
            params: {
              adminAccount: this.ruleForm2.adminAccount,
              adminPassword: this.ruleForm2.adminPassword
            }
          }).then( result => {
            Cookie.set("admin",result.data);
          });
          this.visible = false;
        }else{
          this.$message({
            message: '修改失败',
            type: 'error'
          });
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  },
}
</script>
<style scoped>
  .demo-ruleForm {
    width: 460px;
    padding-top: 25px;
  }
  .el-select {
    width: 100%;
  }
  .card {
    width: 560px;
    padding-bottom: 15px;
    margin: 0px auto;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
